import tagModel from '../../models/tag'
import path from 'path'
module.exports = {
    async list (ctx, next) {
        console.log('----------------获取标签列表 tag/list-----------------------');
        let { pageindex = 1, pagesize = 10} = ctx.request.body;
        try {
          let data = await ctx.findPage(tagModel,{}, {createTime: 0}, {limit: pagesize*1, skip: (pageindex-1)*pagesize});
          ctx.resResult(data)
        }catch (e){
          console.log(e)
          ctx.resResultError(e)
        }
    },
    async add (ctx, next) {
        console.log('----------------添加标签 tag/add-----------------------');
        let paramsData = {...ctx.request.body, createAt: new Date(), updateAt: new Date()}
        try {
            let data = await ctx.findOne(tagModel, {title: paramsData.title})
            if (data) {
                ctx.resResultError('数据已经存在, 请重新添加!')
            } else {
                let data = await ctx.add(tagModel, paramsData);
                ctx.resResult({}, '添加成功!')
            }
        }catch(e) {
            ctx.resResultError(e)
        }
    },
    async update (ctx, next) {
        console.log('----------------更新标签 tag/update-----------------------');
        let paramsData = {...ctx.request.body, updateAt: new Date()};
        console.log(paramsData)
        try {
            // paramsData.html = marked(paramsData.html);
            // model.update({_id: ctx.params.id}, {
            //   ...ctx.request.body,
            //   updateAt: new Date()
            // }
            let data = await ctx.update(tagModel, {_id: ctx.params.id}, paramsData)
            ctx.resResult({}, '更新成功!')
        }catch(e) {
            if (e === '暂无数据') {
                ctx.resResultError(e)
            }
        }
    },
    async del (ctx, next) {
        console.log('----------------删除标签 tag/del-----------------------');
        let id = ctx.request.query.id
        console.log(id)
        try {
            ctx.remove(tagModel, {_id: id})
            ctx.resResult({}, '删除成功!')
        }catch(e){
            ctx.resResultError(e)
        }
    }
}
